<template lang="">
    <div class="container">
    <a-menu v-model="current" mode="horizontal" class="menu" @click="handleClick"> 
      <a-menu-item key="home"> <a-icon type="mail" />首页 </a-menu-item>
  
      <a-sub-menu v-if="currentUser != null && currentUser._role == 'SYS_ADMIN'">
        <span slot="title" class="submenu-title-wrapper"
          ><a-icon type="setting" />人员管理</span
        >
        <a-menu-item-group title="常用">
          <a-menu-item key="people-manager">
            人员信息管理
          </a-menu-item>
          <a-menu-item key="access-manager">
            人员权限管理
          </a-menu-item>
          <a-menu-item key="system-people">
            系统用户统计
          </a-menu-item>
        </a-menu-item-group>
      </a-sub-menu>
       <a-sub-menu v-if="currentUser != null">
        <span slot="title" class="submenu-title-wrapper"
          ><a-icon type="setting" />病人信息管理</span
        >
        <a-menu-item-group title="病人信息管理">
          <a-menu-item key="patient-info" v-if="currentUser != null && (currentUser._role == 'SYS_ADMIN' || currentUser._role == 'DOCTOR')">
            病人信息管理 
          </a-menu-item>
          <a-menu-item key="patient-project">
            病人检查项目管理
          </a-menu-item>
          <a-menu-item key="patient-prescription">
            病人处方管理
          </a-menu-item>
          <a-menu-item key="patient-record">
            <!-- 医生可查看所有患者的病历信息，病人只能查看自己的病历信息 -->
            病人病历管理
          </a-menu-item>
        </a-menu-item-group>
      </a-sub-menu>
      <!-- <a-menu-item key="about">
         <a-icon type="mail" />区块链网络信息
      </a-menu-item> -->

      <a-menu-item key="login" v-if="currentUser == null">
         <a-icon type="user" />登录/注册
      </a-menu-item>

      <a-menu-item key="userInfo" v-if="currentUser != null">
         <a-icon type="user" />当前用户：<span v-if="currentUser != null"> {{currentUser.username}}</span>         
      </a-menu-item>
      <a-menu-item key="logout" v-if="currentUser != null">
         <a-button type="link" @click="logoutView"> 退出 </a-button>      
      </a-menu-item>
    </a-menu>
    </div>
</template>
<script>
import {getCurrentUserInfo, logout} from "@/api/user/user"

export default {
    data(){
        return {
             current: ['mail'],
             currentUser: null,
        }
    },
    methods: {
      handleClick(item){
        // 退出单独处理
        if(item == logout){
          this.logoutView()
          return
        }
        this.$router.push({name:item.key})
      },
      // 获取登录信息（cookie），采用的是sailsjs的会话存储登录状态，因此客户端需要保存cookie
      getUserCookie(){
        //TODO, getCookie
      },

      // 获取当前登录用户信息
      async getCurrentLoginUser(){
        var currentLoginUser = localStorage.getItem("currentLoginUser")
        if(currentLoginUser == undefined || currentLoginUser == "" || currentLoginUser == null || currentLoginUser == 'null'){
          var data = await getCurrentUserInfo()
          if(data == null || data.daat == null){
            this.current = null;
            this.$message.info("用户未登录，请先进行登录")
            return;
          }
          // 获取当前登录用户
          if(data.code != 200){
            this.$message.error("用户未登录，获取当前登录用户信息错误")
            this.currentUser = null
            return;
          }
          this.currentUser = data.data
          console.log("当前登录用户:", data)
          localStorage.setItem("currentLoginUser", JSON.stringify(data.data))
          this.$message.success("获取当前登录用户信息完成")
        }else{
          this.currentUser = JSON.parse(currentLoginUser);
        }
        return null;
      },
      logoutView(){
        logout().then(data=>{
          console.log("logout resp: ", data)
          localStorage.removeItem("currentLoginUser")
          this.$message.success("退出完成")
          this.$router.push({name:"home"})
          location.reload()
        })
        // localStorage.removeItem("currentLoginUser")
      }
    },
    created(){
      this.getCurrentLoginUser();
    }
}
</script>
<style lang="less" scoped>
.menu{
  height: 100% !important;
}

.container{
  height: 100%;
}
</style>